package com.ims.core.doc.service.impl;

import java.util.List;
import com.ims.common.utils.DateUtils;
import com.ims.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ims.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ims.core.doc.domain.TInCollectInvoiceEntry;
import com.ims.core.doc.mapper.TInCollectInvoiceMapper;
import com.ims.core.doc.domain.TInCollectInvoice;
import com.ims.core.doc.service.ITInCollectInvoiceService;

/**
 * 采集增值税发票结果Service业务层处理
 *
 * @author IMS
 * @date 2024-11-10
 */
@Service
public class TInCollectInvoiceServiceImpl implements ITInCollectInvoiceService
{
    @Autowired
    private TInCollectInvoiceMapper tInCollectInvoiceMapper;

    /**
     * 查询采集增值税发票结果
     *
     * @param id 采集增值税发票结果主键
     * @return 采集增值税发票结果
     */
    @Override
    public TInCollectInvoice selectTInCollectInvoiceById(Long id)
    {
        return tInCollectInvoiceMapper.selectTInCollectInvoiceById(id);
    }

    /**
     * 查询采集增值税发票结果列表
     *
     * @param tInCollectInvoice 采集增值税发票结果
     * @return 采集增值税发票结果
     */
    @Override
    public List<TInCollectInvoice> selectTInCollectInvoiceList(TInCollectInvoice tInCollectInvoice)
    {
        return tInCollectInvoiceMapper.selectTInCollectInvoiceList(tInCollectInvoice);
    }

    /**
     * 新增采集增值税发票结果
     *
     * @param tInCollectInvoice 采集增值税发票结果
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertTInCollectInvoice(TInCollectInvoice tInCollectInvoice)
    {
        tInCollectInvoice.setInvoiceId(IdUtils.simpleUUID());
        tInCollectInvoice.setCreateTime(DateUtils.getNowDate());
        int rows = tInCollectInvoiceMapper.insertTInCollectInvoice(tInCollectInvoice);
        insertTInCollectInvoiceEntry(tInCollectInvoice);
        return rows;
    }

    /**
     * 修改采集增值税发票结果
     *
     * @param tInCollectInvoice 采集增值税发票结果
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateTInCollectInvoice(TInCollectInvoice tInCollectInvoice)
    {
        tInCollectInvoice.setUpdateTime(DateUtils.getNowDate());
        tInCollectInvoiceMapper.deleteTInCollectInvoiceEntryByInvoiceId(tInCollectInvoice.getId());
        insertTInCollectInvoiceEntry(tInCollectInvoice);
        return tInCollectInvoiceMapper.updateTInCollectInvoice(tInCollectInvoice);
    }

    /**
     * 批量删除采集增值税发票结果
     *
     * @param ids 需要删除的采集增值税发票结果主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTInCollectInvoiceByIds(Long[] ids)
    {
        tInCollectInvoiceMapper.deleteTInCollectInvoiceEntryByInvoiceIds(ids);
        return tInCollectInvoiceMapper.deleteTInCollectInvoiceByIds(ids);
    }

    /**
     * 删除采集增值税发票结果信息
     *
     * @param id 采集增值税发票结果主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTInCollectInvoiceById(Long id)
    {
        tInCollectInvoiceMapper.deleteTInCollectInvoiceEntryByInvoiceId(id);
        return tInCollectInvoiceMapper.deleteTInCollectInvoiceById(id);
    }

    /**
     * 新增采集增值税发票明细项信息
     *
     * @param tInCollectInvoice 采集增值税发票结果对象
     */
    public void insertTInCollectInvoiceEntry(TInCollectInvoice tInCollectInvoice)
    {
        List<TInCollectInvoiceEntry> tInCollectInvoiceEntryList = tInCollectInvoice.getTInCollectInvoiceEntryList();
        String invoiceId = tInCollectInvoice.getInvoiceId();
        if (StringUtils.isNotNull(tInCollectInvoiceEntryList))
        {
            List<TInCollectInvoiceEntry> list = new ArrayList<TInCollectInvoiceEntry>();
            for (TInCollectInvoiceEntry tInCollectInvoiceEntry : tInCollectInvoiceEntryList)
            {
                tInCollectInvoiceEntry.setEntryId(IdUtils.simpleUUID());
                tInCollectInvoiceEntry.setInvoiceId(invoiceId);
                tInCollectInvoiceEntry.setEntryId(IdUtils.simpleUUID());
                tInCollectInvoiceEntry.setCustId(tInCollectInvoice.getCustId());
                tInCollectInvoiceEntry.setSiteCode(tInCollectInvoice.getSiteCode());
                tInCollectInvoiceEntry.setSiteName(tInCollectInvoice.getSiteName());
                tInCollectInvoiceEntry.setCreateBy(tInCollectInvoice.getCreateBy());
                tInCollectInvoiceEntry.setCreateTime(DateUtils.getNowDate());
                list.add(tInCollectInvoiceEntry);
            }
            if (list.size() > 0)
            {
                tInCollectInvoiceMapper.batchTInCollectInvoiceEntry(list);
            }
        }
    }
}
